package com.news.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.news.common.lang.Const;
import com.news.common.lang.Result;
import com.news.entity.SysUser;
import com.news.mapper.SysUserMapper;
import com.news.service.RegisterService;
import com.news.service.SysUserService;
import com.news.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
//事务回滚
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Autowired
   SysUserService sysUserService;
    @Override
    public Result register(RegisterVo registerVo) {
        String username = registerVo.getUsername();
        //查询是否存在该用户
        SysUser user = sysUserService.findUserByUsername(username);
        if(user != null){
            return Result.fail("用户已存在");
        }
        user = new SysUser();
        user.setNickname(registerVo.getNickname());
        user.setUsername(username);
        user.setPassword(SecureUtil.md5(registerVo.getPassword()+ Const.SALT));
        user.setAvatar(Const.DEFULT_AVATAR);
        user.setPhone(registerVo.getPhone());
        user.setEmail(registerVo.getEmail());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        sysUserService.saveUser(user);
        return Result.succ("");
    }
}
